using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ThresholdAssistant.Autofac;
using ThresholdAssistant.WebAPI.Models;

namespace ThresholdAssistant.WebAPI.Repository
{
    /// <summary>
    /// 阈值明细表仓储
    /// </summary>
    public class ThresholdDetailRepository : BaseRepository, IBaseAutofac
    {
        /// <summary>
        /// 新增
        /// </summary>
        public async Task<bool> AddAsync(ThresholdDetail model)
        {
            string insertSql = @"insert into `t_threshold_detail`
(`threshold_id`,`data_id`)
values
(@ThresholdId,@DataId)";
            return await mydb.ExecuteAsync(insertSql, model);
        }

        /// <summary>
        /// 新增列表
        /// </summary>
        public async Task<bool> AddListAsync(IEnumerable<ThresholdDetail> list)
        {
            if (list == null || list?.Any() == false)
            {
                return false;
            }
            string insertSql = @"insert into `t_threshold_detail`
                                    (`threshold_id`,`data_id`)
                                values
                                    (@ThresholdId,@DataId)";
            return await mydb.ExecuteAsync(insertSql, list);
        }

        /// <summary>
        /// 查询
        /// </summary>
        public async Task<ThresholdDetail> GetInfoAsync(long id)
        {
            string querySql = @"select
 `id`, `threshold_id`, `data_id`
 from `t_threshold_detail`
 where id = @Id;";
            return await mydb.GetFirstOrDefaultAsync<ThresholdDetail>(querySql, new { Id = id });
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="thresholdId"></param>
        /// <returns></returns>
        public async Task<IEnumerable<ThresholdDetail>> GetListAsync(long thresholdId)
        {
            string querySql = @"select
 `id`, `threshold_id`, `data_id`
 from `t_threshold_detail`
 where threshold_id = @thresholdId;";
            return await mydb.GetListAsync<ThresholdDetail>(querySql, new { thresholdId });
        }

        /// <summary>
        /// 查询
        /// </summary>
        public async Task<bool> DeleteAsync(long id)
        {
            string querySql = @" delete from `t_threshold_detail` where id = @Id;";
            return await mydb.ExecuteAsync(querySql, new { Id = id });
        }

        /// <summary>
        /// 查询
        /// </summary>
        public async Task<bool> DeleteByThresholdIdAsync(long thresholdId)
        {
            string querySql = @" delete from `t_threshold_detail` where threshold_id = @ThresholdId;";
            return await mydb.ExecuteAsync(querySql, new { ThresholdId = thresholdId });
        }


        /// <summary>
        /// 修改
        /// </summary>
        public async Task<bool> UpdateAsync(ThresholdDetail model)
        {
            string updateSql = @"update
`t_threshold_detail`
set  `threshold_id`=@ThresholdId, `data_id`=@DataId
 where `id` = @Id";
            return await mydb.ExecuteAsync(updateSql, model);
        }

    }
}
